//
//  HomePageViewController.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/26.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import SwiftFontName
import UIColor_Hex_Swift

class HomePageViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor),
            scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
            scrollView.rightAnchor.constraint(equalTo: view.rightAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
            ])

        let homeTitle = HomeTitleView()
        scrollView.addSubview(homeTitle)
        homeTitle.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            homeTitle.topAnchor.constraint(equalTo: scrollView.topAnchor),
            homeTitle.leftAnchor.constraint(equalTo: scrollView.leftAnchor),
            homeTitle.rightAnchor.constraint(equalTo: scrollView.rightAnchor),
            homeTitle.heightAnchor.constraint(equalToConstant: 300),
            homeTitle.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1)
            ])

        let homeCenter = HomeCenterView()
        scrollView.addSubview(homeCenter)
        homeCenter.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            homeCenter.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 280),
            homeCenter.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 12),
            homeCenter.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: -12),
            homeCenter.heightAnchor.constraint(equalToConstant: 250),
            NSLayoutConstraint(item: homeCenter, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: -24)
            ])

        let homeBottomLeft = HomeBottomLeftView()
        scrollView.addSubview(homeBottomLeft)
        homeBottomLeft.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            homeBottomLeft.topAnchor.constraint(equalTo: homeCenter.bottomAnchor, constant: 12),
            homeBottomLeft.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 12),
            homeBottomLeft.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -40),
            homeBottomLeft.heightAnchor.constraint(equalToConstant: 179),
            homeBottomLeft.widthAnchor.constraint(equalToConstant: (UIScreen.main.bounds.width - 12 * 3) * 0.66)
            ])

        let homeBottomRight = HomeBottomRightView()
        scrollView.addSubview(homeBottomRight)
        homeBottomRight.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            homeBottomRight.topAnchor.constraint(equalTo: homeBottomLeft.topAnchor),
            homeBottomRight.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: -12),
            homeBottomRight.heightAnchor.constraint(equalTo: homeBottomLeft.heightAnchor, multiplier: 1),
            homeBottomRight.leftAnchor.constraint(equalTo: homeBottomLeft.rightAnchor, constant: 12)
            ])
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        hideNavibar(animated: false)
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}



class HomeCenterView: HomeCardView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLabel.text = "饮食"
        setUpViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUpViews() {
        let unitLabel = UILabel()
        addSubview(unitLabel)
        unitLabel.translatesAutoresizingMaskIntoConstraints = false
        unitLabel.font = UIFont(name: FontName.PingFangSCLight, size: 12)
        unitLabel.text = "单位：g"
        NSLayoutConstraint.activate([
            unitLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -20),
            unitLabel.centerYAnchor.constraint(equalTo: titleLabel.centerYAnchor)
            ])

        let pageControll = UIPageControl()
        pageControll.numberOfPages = 2
        pageControll.pageIndicatorTintColor = UIColor("#D8D8D8")
        pageControll.currentPageIndicatorTintColor = UIColor("#03BCB9")
        pageControll.translatesAutoresizingMaskIntoConstraints = false
        addSubview(pageControll)
        NSLayoutConstraint.activate([
            pageControll.centerXAnchor.constraint(equalTo: centerXAnchor),
            pageControll.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0),
            ])

        let scrollView = UIScrollView()
        scrollView.isPagingEnabled = true
        scrollView.alwaysBounceHorizontal = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(scrollView)
        NSLayoutConstraint.activate([
            scrollView.leftAnchor.constraint(equalTo: leftAnchor),
            scrollView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor),
            scrollView.rightAnchor.constraint(equalTo: rightAnchor),
            scrollView.bottomAnchor.constraint(equalTo: pageControll.topAnchor, constant: 0)
            ])

        let firstPage = UIView()
        firstPage.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(firstPage)
        NSLayoutConstraint.activate([
            firstPage.leftAnchor.constraint(equalTo: scrollView.leftAnchor),
            firstPage.topAnchor.constraint(equalTo: titleLabel.bottomAnchor),
            firstPage.bottomAnchor.constraint(equalTo: pageControll.topAnchor),
            firstPage.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1),
            ])

        let secondPage = UIView()
        secondPage.backgroundColor = .yellow
        secondPage.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(secondPage)
        NSLayoutConstraint.activate([
            secondPage.rightAnchor.constraint(equalTo: scrollView.rightAnchor),
            secondPage.topAnchor.constraint(equalTo: titleLabel.bottomAnchor),
            secondPage.bottomAnchor.constraint(equalTo: pageControll.topAnchor),
            secondPage.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1),
            secondPage.leftAnchor.constraint(equalTo: firstPage.rightAnchor)
            ])

        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.translatesAutoresizingMaskIntoConstraints = false
        firstPage.addSubview(stackView)
        NSLayoutConstraint.activate([
            stackView.leftAnchor.constraint(equalTo: firstPage.leftAnchor),
            stackView.rightAnchor.constraint(equalTo: firstPage.rightAnchor),
            stackView.topAnchor.constraint(equalTo: firstPage.topAnchor),
            stackView.bottomAnchor.constraint(equalTo: firstPage.bottomAnchor)
            ])

        let leftCalView = UIView()
        stackView.addArrangedSubview(leftCalView)
        leftCalView.backgroundColor = .red

        let rightCalView = UIStackView()
        rightCalView.axis = .vertical
        rightCalView.distribution = .fillEqually
        stackView.addArrangedSubview(rightCalView)

        let rightTopView = UIView()
        rightCalView.addArrangedSubview(rightTopView)

        let rightBottomView = UIStackView()
        rightBottomView.axis = .horizontal
        rightBottomView.distribution = .fillEqually
        rightCalView.addArrangedSubview(rightBottomView)

        let rightBottomLeftView = UIView()
        rightBottomView.addArrangedSubview(rightBottomLeftView)

        let rightBottomRightView = UIView()
        rightBottomView.addArrangedSubview(rightBottomRightView)

        let residualCal = UILabel()
        residualCal.text = "2052"
        residualCal.textColor = UIColor("#326061")
        residualCal.font = UIFont(name: FontName.PingFangSCMedium, size: 32)
        residualCal.translatesAutoresizingMaskIntoConstraints = false
        rightTopView.addSubview(residualCal)
        NSLayoutConstraint.activate([
            residualCal.centerXAnchor.constraint(equalTo: rightTopView.centerXAnchor),
            residualCal.topAnchor.constraint(equalTo: rightTopView.topAnchor)
            ])

        let residualDescCal = UILabel()
        residualDescCal.text = "还可摄入"
        residualDescCal.textColor = UIColor("#333333")
        residualDescCal.font = UIFont(name: FontName.PingFangSCRegular, size: 12)
        residualDescCal.translatesAutoresizingMaskIntoConstraints = false
        rightTopView.addSubview(residualDescCal)
        NSLayoutConstraint.activate([
            residualDescCal.centerXAnchor.constraint(equalTo: rightTopView.centerXAnchor),
            residualDescCal.topAnchor.constraint(equalTo: residualCal.bottomAnchor)
            ])

        let eatenCal = UILabel()
        eatenCal.text = "320"
        eatenCal.textColor = UIColor("#326061")
        eatenCal.font = UIFont(name: FontName.PingFangSCRegular, size: 18)
        eatenCal.translatesAutoresizingMaskIntoConstraints = false
        rightBottomLeftView.addSubview(eatenCal)
        NSLayoutConstraint.activate([
            eatenCal.topAnchor.constraint(equalTo: rightBottomLeftView.topAnchor),
            eatenCal.centerXAnchor.constraint(equalTo: rightBottomLeftView.centerXAnchor)
            ])

        let eatenDescCal = UILabel()
        eatenDescCal.text = "摄入"
        eatenDescCal.textColor = UIColor("#333333")
        eatenDescCal.font = UIFont(name: FontName.PingFangSCRegular, size: 12)
        eatenDescCal.translatesAutoresizingMaskIntoConstraints = false
        rightBottomLeftView.addSubview(eatenDescCal)
        NSLayoutConstraint.activate([
            eatenDescCal.topAnchor.constraint(equalTo: eatenCal.bottomAnchor),
            eatenDescCal.centerXAnchor.constraint(equalTo: rightBottomLeftView.centerXAnchor)
            ])

        let costCal = UILabel()
        costCal.text = "320"
        costCal.textColor = UIColor("#326061")
        costCal.font = UIFont(name: FontName.PingFangSCRegular, size: 18)
        costCal.translatesAutoresizingMaskIntoConstraints = false
        rightBottomRightView.addSubview(costCal)
        NSLayoutConstraint.activate([
            costCal.topAnchor.constraint(equalTo: rightBottomRightView.topAnchor),
            costCal.centerXAnchor.constraint(equalTo: rightBottomRightView.centerXAnchor)
            ])

        let costDescCal = UILabel()
        costDescCal.text = "摄入"
        costDescCal.textColor = UIColor("#333333")
        costDescCal.font = UIFont(name: FontName.PingFangSCRegular, size: 12)
        costDescCal.translatesAutoresizingMaskIntoConstraints = false
        rightBottomRightView.addSubview(costDescCal)
        NSLayoutConstraint.activate([
            costDescCal.topAnchor.constraint(equalTo: costCal.bottomAnchor),
            costDescCal.centerXAnchor.constraint(equalTo: rightBottomRightView.centerXAnchor)
            ])
    }
}
